package com.example.basenumberconverter;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Toast;


public class MainActivity extends Activity {
	
   	private Button btnDec, btnBin, btnOct, btnHex; 
	 private EditText edtInput, edtResult; 
	 private Spinner spinner; 

 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setWidgets(); 
        
        
        View.OnClickListener onClick = new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
        // TODO Auto-generated method   
        	if (v == btnDec ) { 
        		BaseConvertor base = new BaseConvertor (spinner.getSelectedItem().toString(), "DEC", 
        		edtInput.getText().toString()); 
        		 try {
					edtResult.setText(base.getNumber());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					
					e.printStackTrace();
				} 
        		 } 
        	else if (v == btnBin) { 
        		 BaseConvertor base = new BaseConvertor(spinner 
        		 .getSelectedItem().toString(), "BIN", 
        		edtInput 
        		 .getText().toString()); 
        		 try {
					edtResult.setText(base.getNumber());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
        		 try {
					if(base.getNumber().equals("data don't only match : [0/1]")){
						  Toast.makeText(getBaseContext(), "aa",Toast.LENGTH_LONG).show();
					 }
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
     		 
        		 } 
        	else if (v == btnOct) { 
        			 BaseConvertor base = new  BaseConvertor(spinner 
        		 .getSelectedItem().toString(),"OCT", 
        		edtInput.getText().toString()); 
        			 try {
						edtResult.setText(base.getNumber());
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
        		 } 
        	else if (v == btnHex) { 
        		 BaseConvertor base = new BaseConvertor(spinner 
        		 .getSelectedItem().toString(), "HEX", 
        		edtInput 
        		 .getText().toString()); 
        		 try {
					edtResult.setText(base.getNumber());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
        		 } 
        		 
        
        } 
}; 
        
        btnDec.setOnClickListener(onClick); 
        btnBin.setOnClickListener(onClick); 
        btnOct.setOnClickListener(onClick); 
        btnHex.setOnClickListener(onClick); 


    }
    
    
    
    private void setWidgets() { 
    	 btnDec = (Button) findViewById(R.id.btnDec); 
    	 btnBin = (Button) findViewById(R.id.btnBin); 
    	 btnOct = (Button) findViewById(R.id.btnOct); 
    	 btnHex = (Button) findViewById(R.id.btnHex); 
    	 spinner=(Spinner) findViewById(R.id.spBase); 
    	 edtResult = (EditText)findViewById(R.id.edtResult); 
    	 edtInput  = (EditText)findViewById(R.id.edtInput); 
    	 
    	 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( 
    	 this, R.array.base_string, android.R.layout.simple_spinner_item); 
    	 
    	 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    	 spinner.setAdapter(adapter); 
    	 } 


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
